package com.woniuxy.qiantai.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.qiantai.entity.Book;
import com.woniuxy.qiantai.entity.Booktype;
import com.woniuxy.qiantai.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author woniumrwang
 * @since 2023-01-08 07:35:17
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/topNBooks")
    @ResponseBody
    public List<Book> topNBooks(Integer pageSize) {
        Page<Book> topNBook = bookService.getTopNBook(1, pageSize);
        List<Book> records = topNBook.getRecords();
        System.out.println(records);
        return records;
    }

    @RequestMapping("selectBookById")
    public String selectById(Integer id, Model model) {
        Book book = bookService.getById(id);
        model.addAttribute("book", book);
        return "selectBookById";
    }

    @RequestMapping("selectBooksByTypeId")
    public String selectBooksByTypeId(Integer typeId, Model model) {
        model.addAttribute("typeId", typeId);
        return "selectBooksByTypeId";
    }

    @RequestMapping("pageBooksByTypeId")
    @ResponseBody
    public Page<Book> pageBooksByTypeId(@RequestParam(defaultValue = "9") Integer pageSize, @RequestParam(defaultValue = "1") Integer currentPage, Integer typeId) {
        Page<Book> pageBookResult = bookService.getPageBooksByTypeId(currentPage, pageSize, typeId);
        return pageBookResult;
    }
}

